/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Screen;

import ClassFrame.Button;
import ClassFrame.InputKey;
import ClassFrame.Map;
import ClassFrame.Popup;
import ClassFrame.PopupOnclickListener;
import ClassFrame.Resource;
import GamePlay.CanvasGame;
import javax.microedition.lcdui.Graphics;

/**
 *
 * @author QuyetNM1
 */
public class ChoosePiece extends IScreen implements PopupOnclickListener {

    private long startTime;
    private long currTime;
    private int numOfRightAnswer = 0;
    public Button specialPiece, btnExit;
    public Popup popup;

    public ChoosePiece(CanvasGame _canvas) {
        super(_canvas);
    }

    public void initComponents() {
//        canvas.getMap().initRound(Map.round);
        specialPiece = new Button("", Resource.IMG_BTN_PIECE_SPECIAL, 2, 290);
        btnExit = new Button("Thoát", Resource.IMG_BTN_LT, 160, 285);
        popup = new Popup(1, canvas, this, Resource.IMG_BG_NOTICE, "Bạn có chắc chắn muốn thoát?", "Có", "Không");
        for (int i = 0; i < canvas.getMap().getqArray().length; i++) {
            for (int j = 0; j < canvas.getMap().getqArray()[i].length; j++) {
                if (canvas.getMap().getqArray()[i][j].isRight()) {
                    numOfRightAnswer++;
                }
            }
        }
        if (numOfRightAnswer < 4 || Map.round != Map.ROUND1) {
            specialPiece.setState(Button.DISABLE);
        }
        if (isAnswerAll()) {
            specialPiece.setState(Button.ENABLE);
        }
    }

    public void load_screen() {
        canvas.getResource().loadArray(Resource.choosePieceArrayImages);
    }

    public void un_load_screen() {
        canvas.getResource().unLoadArray(Resource.choosePieceArrayImages);
    }

    public void keyEvent(int keycode, int event) {
        switch (event) {
            case InputKey.KEY_EVENT_DOWN:
//                canvas.setScreen(new Welcome2(canvas));
                break;
        }
    }

    public void pointerEvent(int x, int y, int event) {
        if (popup.isShowing()) {
            popup.pointerEvent(x, y, event);
        } else {
            switch (event) {
                case InputKey.POINTER_EVENT_DOWN:
                    if (canvas.checkButton(x, y, btnExit)) {
                        btnExit.setState(Button.HOLD_CLICK);
                        canvas.getResource().playSound(Resource.SOUND_BUTTON, 1);
                    } else if (canvas.checkButton(x, y, specialPiece)) {
                        specialPiece.setState(Button.HOLD_CLICK);
                        canvas.getResource().playSound(Resource.SOUND_BUTTON, 1);
                    } else {
                        for (int i = 0; i < canvas.getMap().getpArray().length; i++) {
                            for (int j = 0; j < canvas.getMap().getpArray()[i].length; j++) {
                                if (canvas.getMap().getpArray()[i][j].isClick(x, y)) {
                                    canvas.getMap().getpArray()[i][j].setState(Button.HOLD_CLICK);
                                    canvas.getResource().playSound(Resource.SOUND_BUTTON, 1);
                                    break;
                                }
                            }
                        }
                    }
                    break;
                case InputKey.POINTER_EVENT_UP:
                    if (canvas.checkButton(x, y, btnExit) && btnExit.getState() == Button.HOLD_CLICK) {
                        popup.show();
                    } else if (canvas.checkButton(x, y, specialPiece) && specialPiece.getState() == Button.HOLD_CLICK) {
                        Map.piece = 10;
                        System.out.println("Piece = " + Map.piece);
                        canvas.setScreen(new Game(canvas, canvas.getMap().getSpecialQuestion()));
                    } else {
                        for (int i = 0; i < canvas.getMap().getpArray().length; i++) {
                            for (int j = 0; j < canvas.getMap().getpArray()[i].length; j++) {
                                if (canvas.getMap().getpArray()[i][j].isClick(x, y) && canvas.getMap().getpArray()[i][j].getState() == Button.HOLD_CLICK) {
                                    if (!canvas.getMap().getqArray()[i][j].isAnswered()) {
                                        canvas.getMap().getqArray()[i][j].setAnswered();
                                        Map.piece = j * canvas.getMap().getpArray().length + i + 1;
                                        System.out.println("Piece = " + Map.piece);
                                        canvas.setScreen(new Game(canvas, canvas.getMap().getqArray()[i][j]));
                                        break;
                                    }
                                }
                            }
                        }

                        for (int i = 0; i < canvas.getMap().getpArray().length; i++) {
                            for (int j = 0; j < canvas.getMap().getpArray()[i].length; j++) {
                                canvas.getMap().getpArray()[i][j].setState(Button.ENABLE);
                            }
                        }
                    }
                    if (specialPiece.getState() != Button.DISABLE && Map.round == Map.ROUND1) {
                        specialPiece.setState(Button.ENABLE);
                    } else {
                        specialPiece.setState(Button.DISABLE);
                    }
                    btnExit.setState(Button.ENABLE);
                    break;
            }
        }
    }

    public void update() {
    }

    public void paint(Graphics g) {
        g.drawImage(canvas.getResource().getIMG(Resource.IMG_BG_PLAY), 0, 0, Graphics.TOP | Graphics.LEFT);
        g.drawImage(canvas.getResource().getIMG(Resource.IMG_FRAME), 20, 75, Graphics.TOP | Graphics.LEFT);
        canvas.getImage().drawNumber(g, canvas, Resource.IMG_NUMBER, Game.score, 180, 6, ClassFrame.Image.LEFT);

        canvas.getMap().drawPieceMap(g);
        if (numOfRightAnswer >= 4 || isAnswerAll()) {
            specialPiece.drawButton(g, canvas);
        }

        if (Map.round == Map.ROUND3) {
            Game.downClock();
            canvas.getImage().drawNumber(g, canvas, Resource.IMG_NUMBER, Game.time, 88, 6, ClassFrame.Image.CENTER);

            if (Game.time <= 0) {
                canvas.setScreen(new Game(canvas, null));
                if (Game.scoreRound3 < 1000) {
                    Game.gameState = Game.STATE_HETGIO;
                } else {
                    Game.gameState = Game.STATE_STAGE_COMPLETE;
                }
            }
        }
        btnExit.drawButton(g, canvas);
        popup.draw(g);
    }

    public boolean isAnswerAll() {
        for (int i = 0; i < canvas.getMap().getpArray().length; i++) {
            for (int j = 0; j < canvas.getMap().getpArray()[i].length; j++) {
                if (!canvas.getMap().getqArray()[i][j].isAnswered()) {
                    return false;
                }
            }
        }
        return true;
    }

    public void onClickPositiveButton(int id) {
        popup.dismiss();
        canvas.setScreen(new Menu(canvas));
    }

    public void onClickNegativeButton(int id) {
        popup.dismiss();
    }

    public int getNumOfRightAnswer() {
        return numOfRightAnswer;
    }

    public void setNumOfRightAnswer(int numOfRightAnswer) {
        this.numOfRightAnswer = numOfRightAnswer;
    }
}
